package com.sunday.aomicaichang.ui.my;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.widgets.PayPwdEditText;
import com.sunday.member.entity.Member;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/6.
 */

public class PaySetting3Activity extends BaseActivity {

    @Bind(R.id.edt_password)
    PayPwdEditText edtPassword;
    @Bind(R.id.btn_ok)
    TextView btnOk;

    private String code;
    private String password;
    private String checkPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay3);
        ButterKnife.bind(this);
        code = getIntent().getStringExtra("code");
        password=getIntent().getStringExtra("password");
        init();

    }

    private void init() {
        edtPassword.initStyle(R.drawable.edit_by_bg, 6, 0.33f, R.color._999, R.color._999, 20);
        edtPassword.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                checkPwd=str;
            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });
    }

    private void check(){
        if(checkPwd==null){
            ToastUtils.showToast(mContext, "请输入密码");
            return;
        }
        if(!password.equals(checkPwd)){
            ToastUtils.showToast(mContext,"密码与确认密码不符！");
            return;
        }
        getData();
    }
    private void getData() {
        String nonce = MD5Utils.random();
        String[] str1 = new String[]{ "payPassword", "code", "nonce"};
        String[] str2 = new String[]{ password, code, nonce};
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().setPayPassword( password, code,
                nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    ToastUtils.showToast(mContext, "设置成功");
                    Member member = (Member) SharePerferenceUtils.getIns(mContext).getOAuth();
                    member.setIsPayPassword(1);
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
}

